package org.cjprods.icerupt;

import com.hackoeur.jglm.Mat3;
import com.hackoeur.jglm.Vec3;

public class bullet
{
	Vec3 position = new Vec3();
	Vec3 velocity;
	boolean dead = true;
	float gun_force = 0;
	float age_ = 0;
	float size_ = 0;
	boolean old_holding = false;
	static float gravity = -0.01f;

	public Vec3 get() { return position; }	
	public float force() { return gun_force; }
	public float size() { return size_; }
	public float age() { return age_; }
	
	public void update(Vec3 tank_pos)
	{
		if (dead) {
			position = tank_pos;
			return;
		}
	
		velocity = velocity.add(new Vec3(0, gravity, 0)).scale(0.99f);
		position = position.add(velocity);
		age_ += 0.02f;
		if (age_ > 1) age_ = 1;
		else 
		if (age_ > 0.9) {
			size_ += 1f;
			velocity = new Vec3();
		}
		
		if (age_ == 1) {
			dead = true;
			position = tank_pos;
			age_ = 0;
			size_ = 0;
		}
		if (position.getY() < -2) {
			velocity = new Vec3(velocity.getX(), -velocity.getY() * 0.2f, velocity.getZ());
			position = new Vec3(position.getX(), -2, position.getZ());
		}
		if (position.getZ() < 1) {
			velocity = new Vec3(velocity.getX(), velocity.getY() * 0.5f, -velocity.getZ() * 0.5f);
			position = new Vec3(position.getX(), position.getY(), 1);
		}
	
	}
	
	public void shoot(Mat3 gun_axis, Vec3 tank_pos, boolean holding)
	{
		if (holding) {
			gun_force += 0.04f;
			if (gun_force > 1) gun_force = 1;
			size_ = gun_force;
		}
		else if (old_holding) {
			gun_force = 0;
			Vec3 gun = gun_axis.multiply(new Vec3(0, 0, -1));
			if (dead) {
				position = tank_pos.add(gun.scale(1.618f*0.25f*2.0f));
				velocity = new Vec3();
			}
			velocity = velocity.add(gun.scale(gun_force*10.0f + 1.0f));
			dead = false;
		}
		old_holding = holding;
	}
}
